package com.hsp.encap;

public class Encapsulation01 {
    public static void main(String[] args) {
        Person person = new Person();
        person.setName("jack");
        person.setAge(190);
        person.setSalary(6000);
        System.out.println(person.info());
        System.out.println(person.getSalary());


        Person smith = new Person("smith", 20, 50000);
        System.out.println("=====simth的信息====");
        System.out.println(smith.info());

    }
}

class Person {
    public String name;
    private int age;//私有化。在其他里面无法修改
    private double salary;


    public Person() {
    }
    //有三个属性的构造器
    public Person(String name, int age, double salary) {
//        this.name = name;
//        this.age = age;
//        this.salary = salary;
        setName(name);
        setAge(age);
        setSalary(salary);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        //加入对数据的校验
        if (name.length()>1&&name.length()<=6) {
            this.name = name;
        }
        else {
            System.out.println("年龄长度不对，需要2-6字符");
            this.name = "无名人士";
        }
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age >= 1 && age <= 120) {
            this.age = age;
        }
        else {
            System.out.println("年龄需要在1-120，给你一个默认年龄18");
            this.age = 18;//给一个默认年龄
        }
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public String info() {
        return "信息为 name=" + name + " age=" + age + " salary=" + salary;
    }
}